<?php
    // part of the pages controller. Only used to display pages
    // created in the admin section
    class ContentController extends Application {
        
        function setup()
        {
            $this->actions = array('index');
            use_helper('admin/pages');
            use_model('pages','page_content');
        }
        
		function index() 
		{
		    $pages = new Pages();
		    // get page by id
		    if ( !empty($this->params['page']) )
		    {	
		        $index_name = $this->params['page']	;
	        } else {
	            $index_name = $pages->get_index_page();
		        if ( empty($index_name) )
		        {
		            $this->assign('msg',"You do not have a page marked as your index page! ".link_to('Do that here',array('controller'=>'admin/pages' ))
		                         );
		            return;
		        }
	        }
		    $id    = $pages->find( array(
	                                'search'=>'first',
	                                'conditions'=>array(
	                                                'index_name=?',rawurlencode($index_name)
	                                                   ),
	                                'fields'=>'id'
	                                    ) 
	                             );
	        $id    = (int) $id['id'];
	        if ( $pages->load($id) )
	        {
	            $this->assign('page',$pages->fields);
	            $this->assign('admin_edit',false);
	            
	            // render page
	            if ( $pages->get('disable_default_layout') )
	            {
	                $this->use_layout(false);
	            } else {
	                $this->use_layout('application.php');
	            }
	            $this->assign('meta_keywords',$pages->get('meta_keywords'));
	            $this->assign('meta_description',$pages->get('meta_description'));
	            $title = $pages->get('meta_title');
	            $title = empty($title) ? $pages->get('title') : $title;
	            $this->assign('meta_page_title',$title);
	            $this->render(array(
	                        'template'=>VIEWS.'/admin/pages/templates/'.$pages->get('template_file'),
	                        'file'=>true
	                        ));
	            
	        } else {
	            $this->render(array('inline'=>'page failed to load.'));
	        }
		}

        
    }

?>